Saturday, June 25, 2016
Some programming languages you might have heard of:
Creating variables
baby <- 'Meredith' sculpture <- 10
baby
## [1] "Meredith"
sculpture
## [1] 10
1, 2, 31.25, 3.783 + 2i"Research", "Platforms"TRUE and FALSE
vector1 <- 1:10
vector2 <- c("learning", "to", "code", "is", "fun")
vector1
## [1] 1 2 3 4 5 6 7 8 9 10
vector2
## [1] "learning" "to" "code" "is" "fun"
PlatoonLeads <- data.frame(
list(
platoon = c("Data Wranglers",
"Data Miners",
"Data Vizards",
"Cadventurers"),
name = c("Kerry Halupka",
"Kim Doyle",
"Isabell Kiral-Kornek",
"Louise van der Werff")))
PlatoonLeads
## platoon name ## 1 Data Wranglers Kerry Halupka ## 2 Data Miners Kim Doyle ## 3 Data Vizards Isabell Kiral-Kornek ## 4 Cadventurers Louise van der Werff
shoppingList <- list(
breakfast = c("cereal", "milk", "orange juice", "banana"),
lunch = c("bread", "cheese", "tomato"),
dinner = c("frozen pizza", "chocolate mousse")
)
shoppingList
## $breakfast ## [1] "cereal" "milk" "orange juice" "banana" ## ## $lunch ## [1] "bread" "cheese" "tomato" ## ## $dinner ## [1] "frozen pizza" "chocolate mousse"
IMDBRatings = {"Game of Thrones": 9.4,
"Sherlock": 9.2,
"Firefly": 9.1,
"Friends": 8.9}
for movie, rating in IMDBRatings.items():
print movie + ': ' + str(rating)
## Firefly: 9.1 ## Friends: 8.9 ## Sherlock: 9.2 ## Game of Thrones: 9.4
If statements
If statements
hungry <- sample(c(TRUE, FALSE), 1)
if (hungry){
print("GIVE ME A TIM TAM!!!")
} else {
print("No tim tam for me, thanks!")
}
## [1] "GIVE ME A TIM TAM!!!"
For loops
For loops
for (beats in 1:4){
print("bring left foot to right foot")
print("step left foot back to original position")
print("bring right foot to left foot")
print("step left foot back to original position")
}
## [1] "bring left foot to right foot" ## [1] "step left foot back to original position" ## [1] "bring right foot to left foot" ## [1] "step left foot back to original position" ## [1] "bring left foot to right foot" ## [1] "step left foot back to original position" ## [1] "bring right foot to left foot" ## [1] "step left foot back to original position" ## [1] "bring left foot to right foot" ## [1] "step left foot back to original position" ## [1] "bring right foot to left foot" ## [1] "step left foot back to original position" ## [1] "bring left foot to right foot" ## [1] "step left foot back to original position" ## [1] "bring right foot to left foot" ## [1] "step left foot back to original position"
Functions
Functions
moodCalendar <- function(day){
if (day %in% c("Friday", "Saturday", "Sunday")){
print(paste(day, "is a happy day"))
} else if (day %in% c("Monday", "Tuesday")){
print(paste(day, "is a grumpy day"))
} else {
print(paste(day, "is a neutral day"))
}
}
moodCalendar("Monday")
## [1] "Monday is a grumpy day"
moodCalendar("Thursday")
## [1] "Thursday is a neutral day"